import pygame
import jiong
import tool
from player import *
from ghost import *

class Block(jiong.Jiong):
    # rect : > 0
    # color: not none
    def __init__(self, rect, color):
        rect = pygame.Rect(rect)
        image = tool.load_shape('rect', (rect.width, rect.height), color)
        jiong.Jiong.__init__(self, image, (rect.left, rect.top))

class Bound(jiong.Jiong):
    # rect : > 0
    def __init__(self, rect):
        rect = pygame.Rect(rect)
        image = tool.load_shape('rect', (rect.width, rect.height), (0, 0, 0))
        image.set_colorkey((0, 0, 0), pygame.RLEACCEL)
        jiong.Jiong.__init__(self, image, (rect.left, rect.top))

class Map(object):    
    ITEM_BLOCK = 1
    ITEM_PLAYER = 2
    ITEM_GHOST = 3
    
    # level (width, height, itemlist)
    # itemlist (type, rect, color)
    # viewsize (width, height)
    def __init__(self, level, viewsize):
        self.__width = level['width']
        self.__height = level['height']
        self.__view = pygame.Rect((0, 0), viewsize)
        self.__bgcolor = level['bgcolor']
        self.__map = pygame.Surface((self.__width, self.__height))
        self.__block = []
        self.__player = None
        self.__ghost = []
        self.__bound = []
        for item in level['itemlist']:
            if item[0] == Map.ITEM_BLOCK:
                self.__block.append(Block(item[1], item[2]))
            elif item[0] == Map.ITEM_PLAYER:
                self.__player = Player(item[1])
            elif item[0] == Map.ITEM_GHOST:
                self.__ghost.append(Ghost(item[1], item[2], item[3]))
        self.__generate_bound()
        self.__draw_map()
        self.center_view(level['start'])
    
    def __generate_bound(self):
        self.__bound.append(Bound((0, -1, self.__width, 1))) # top
        self.__bound.append(Bound((0, self.__height, self.__width, 1))) # bottom
        self.__bound.append(Bound((-1, 0, 1, self.__height))) # left
        self.__bound.append(Bound((self.__width, 0, 1, self.__height))) # right

    
    def __draw_map(self):
        self.__map.fill(self.__bgcolor)
        for item in self.__block:
            self.__map.blit(item.image, item.rect)
        
    def center_view(self, pos):
        self.__view.move_ip(pos[0] - self.__view.centerx, pos[1] - self.__view.centery)
        if self.__view.left < 0: self.__view.move_ip(-self.__view.left, 0)
        if self.__view.right > self.__width: self.__view.move_ip(self.__width - self.__view.right, 0)
        if self.__view.top < 0: self.__view.move_ip(0, -self.__view.top)
        if self.__view.bottom > self.__height: self.__view.move_ip(0, self.__height - self.__view.bottom)

    def get_size(self):
        return (self.__width, self.__height)

    def get_view(self):
        return self.__view
    
    def get_map(self):
        return self.__map
        
    def get_block(self):
        return self.__block
    
    def get_player(self):
        return self.__player
    
    def get_ghost(self):
        return self.__ghost   

    def get_bound(self):
        return self.__bound   
     
    def map2view(self, rect):
        return rect.move(-self.__view.left, -self.__view.top)
    
    def view2map(self, rect):
        return rect.move(self.__view.left, self.__view.top)
    
level = ( {}, # ignore 0
          {# level 1
           'width': 1800,
           'height': 800,
           'start': (50, 350),
           'bgcolor': (100, 50, 25),
           'itemlist': (# type, (x, y, width, height), color
                        (Map.ITEM_BLOCK, (0, 400, 300, 400), (100, 100, 100)),
                        (Map.ITEM_BLOCK, (400, 400, 200, 400), (100, 100, 100)),
                        (Map.ITEM_BLOCK, (650, 350, 250, 20), (100, 100, 100)),
                        (Map.ITEM_BLOCK, (950, 300, 50, 20), (100, 100, 100)),
                        (Map.ITEM_BLOCK, (1100, 400, 200, 20), (100, 100, 100)),
                        (Map.ITEM_BLOCK, (1250, 600, 550, 200), (100, 100, 100)),
                        (Map.ITEM_PLAYER, (50, 350)),
                        (Map.ITEM_GHOST, (335, 315), (0, 2), (0, 100)), 
                        (Map.ITEM_GHOST, (1000, 350), (2, -1), (160, 80)),
                        (Map.ITEM_GHOST, (1250, 450), (3, 0), (250, 0)),
                        (Map.ITEM_GHOST, (1350, 550), (4, 0), (350, 0)),                       
                        )
           }
          ) 
        
        
        
